<template>
  <div class="art-container">
    <el-form :model="formArticle" ref="formArticle" :rules="formRules" >
      <el-form-item label="文章标题" size="small" prop="title">
        <el-select
          v-model="formArticle.title"
          placeholder="请输入文章标题"
          style="width: 88%"
        >
          <el-option
            v-for="item in tableData"
            :key="item.id"
            :label="item.title"
            :value="item.title"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="文章内容" prop="articleBody">
        <quill-editor v-model="formArticle.articleBody"></quill-editor>
      </el-form-item>
      <el-form-item label="视频地址" prop="videoURL">
        <el-input
          v-model="formArticle.videoURL"
          style="width: 89%"
          placeholder="请输入视频地址"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="cancelSubmit">取消</el-button>
        <el-button type="primary" @click="addSubmit">确认</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { add, update } from '@/api/hmmm/articles.js'
export default {
  props: {
    tableData: {
      type: Array
    },
    isEdit: {
      type: Boolean
    },
    row: {
      type: Object
    }
  },
  data () {
    return {
      formArticle: {
        title: '',
        articleBody: '',
        videoURL: ''
      },

      formRules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ]
      }
    }
  },
  created () {},
  watch: {
    isEdit: {
      handler (newVal) {
        if (newVal) {
          this.formArticle = JSON.parse(JSON.stringify(this.row))
        }
      },
      immediate: true // 初次进入,立即监听
    }
  },
  methods: {
    cancelSubmit () {
      //  关闭对话框
      this.$emit('update:dialogAddVisible', false)
      // 清空表单
      this.formArticle = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
    },
    addSubmit () {
      this.$refs.formArticle.validate(async (valid) => {
        if (valid) {
          // isEdit为false的时候,为新增
          if (!this.isEdit) {
            const res = await add(this.formArticle)
            // console.log(res, 8888)
            if (res.status === 200) {
              //  关闭对话框
              this.$emit('update:dialogAddVisible', false)
              // 清空表单
              this.formArticle = {
                title: '',
                articleBody: '',
                videoURL: ''
              }
              // 刷新
              this.$emit('success')
            }
          } else {
            // isEdit为true的时候,为修改
            const res = await update(this.formArticle)
            if (res.status === 200) {
              // console.log(res, 66666)
            //  关闭对话框
              this.$emit('update:dialogAddVisible', false)
              // 清空表单
              this.formArticle = {
                title: '',
                articleBody: '',
                videoURL: ''
              }
              // 刷新
              this.$emit('success')
            }
          }
        }
      })
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .quill-editor {
  width: 88%;
  margin-left: 76px;
}
::v-deep .ql-container {
  min-height: 200px;
}
::v-deep .ql-formats .ql-header {
  display: none;
}
::v-deep .ql-formats .ql-script {
  display: none;
}
::v-deep .ql-formats .ql-indent {
  display: none;
}
::v-deep .ql-formats .ql-direction {
  display: none;
}
::v-deep .ql-formats .ql-size {
  display: none;
}
::v-deep .ql-formats .ql-color {
  display: none;
}
::v-deep .ql-formats .ql-background {
  display: none;
}
::v-deep .ql-formats .ql-font {
  display: none;
}
::v-deep .ql-formats .ql-align {
  display: none;
}
::v-deep .ql-formats .ql-clean {
  display: none;
}

::v-deep .ql-formats .ql-video {
  display: none;
}
::v-deep .ql-toolbar.ql-snow .ql-formats {
  margin-right: 0px;
}
::v-deep.ql-toolbar.ql-snow {
  padding: 0px;
}
</style>
